/** 
 * Common.java
 * 
 * 1.0
 * 
 * 04/07/2014
 * 
 * Copyright (c) 2013 FPT Software.
 * FPT Building, Street No1, DaNang Industrial Zone, AnDon, 
 * AnHaiBac Ward, SonTra District, DaNang City.
 * All rights reserved.
 */
package com.jms.fu.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Class to implement common operating methods
 * 
 * @author Design Framework Group
 */
public class Common {

	/**
	 * Method to encrypt Password
	 * 
	 * @return user String
	 * 
	 */
	public static String encryptPassword(String password)
			throws NoSuchAlgorithmException, UnsupportedEncodingException {
		MessageDigest m = MessageDigest.getInstance("MD5"); // Use MessageDisget
															// to encrypt the
															// password string
															// by MD5
		m.update(password.getBytes("UTF-8"));
		byte[] b = m.digest();
		StringBuffer result = new StringBuffer("");
		for (int i = 0; i < b.length; i++) {
			result.append(Integer.toHexString((0x000000ff & b[i]) | 0xffffff00)
					.substring(6));
		}
		return result.toString();
	}

	/**
	 * Method to get Property
	 * 
	 * @return user String
	 * 
	 */
	public static String getProperty(String propertyName) throws IOException {
		Properties prop = new Properties(); // Use properties to get property
											// with given name
		prop.load(Common.class.getResourceAsStream("Resources.properties"));
		return prop.getProperty(propertyName);
	}

	/**
	 * Method to get time now
	 * 
	 * @return user String
	 * 
	 */
	public static String getTimeNow() {
		DateFormat dateFormat = new SimpleDateFormat(Constant.DATE_FORMAT);
		Date date = new Date();
		return dateFormat.format(date);
	}
}
